package com.fh.adminrole.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.adminrole.entity.UmsAdminRole;
import com.fh.adminrole.service.IUmsAdminRoleService;
import com.fh.result.ResultObject;
import com.fh.search.AdminRoleBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author F
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/ums/adminRole")
public class UmsAdminRoleController {

    @Autowired
    private IUmsAdminRoleService adminRoleService;

    @PostMapping
    public ResultObject saveRoleToAdmin(@RequestBody AdminRoleBO adminRoleBO) {
        adminRoleService.saveRoleToAdmin(adminRoleBO);
        return ResultObject.success();
    }

    @GetMapping("/{userId}")
    public ResultObject getRoleByAdminId(@PathVariable("userId") Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_id", userId);
        queryWrapper.select("role_id");
        List<UmsAdminRole> list = adminRoleService.list(queryWrapper);
        List<Long> roleIdList = list.stream().map(x -> x.getRoleId()).collect(Collectors.toList());
        return ResultObject.success(roleIdList);
    }
}